package com.easyedu.dean.service;

import com.easyedu.core.dao.BuildingAddressDao;
import com.easyedu.core.dao.ClassRoomDao;
import com.easyedu.core.entity.DbError;
import com.easyedu.core.entity.DeanServiceError;
import com.easyedu.core.entity.po.BuildingAddress;
import com.easyedu.core.entity.po.Classroom;
import com.easyedu.core.exception.EasyEduException;
import com.easyedu.dean.entity.dto.request.ReqCreateClassroom;
import com.easyedu.dean.entity.dto.request.ReqUpdateClassRoom;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author lmr
 **/
@Service
public class ClassRoomService {

     private final ClassRoomDao classRoomDao;
     private final BuildingAddressDao buildingAddressDao;

     public ClassRoomService(ClassRoomDao classRoomDao, BuildingAddressDao buildingAddressDao) {

          this.classRoomDao = classRoomDao;
          this.buildingAddressDao = buildingAddressDao;
     }

     public List<Classroom> getClassroom() {

          return classRoomDao.getClassroom();
     }

     @Transactional(rollbackFor = Exception.class)
     public List<String> createClassroom(List<ReqCreateClassroom> reqCreateClassrooms) {

          List<String> hasClassroom = new ArrayList<>();
          for (ReqCreateClassroom classroom : reqCreateClassrooms) {
               String belongBuild = classroom.getBelongBuild();
               BuildingAddress buildByName = buildingAddressDao.getByName(belongBuild);
               if (buildByName == null) {
                    throw new EasyEduException(DeanServiceError.ERR_ACADEMIC_BUILDING_NOT_EXIST);
               }
               Classroom classroomByName = classRoomDao.getClassroomByName(classroom.getClassroomName());
               if (classroomByName != null) {
                    hasClassroom.add(classroomByName.getClassroomName());
               } else {

                    Integer oneClassroom = classRoomDao.createOneClassroom(
                            Classroom.builder().classroomName(classroom.getClassroomName())
                                    //TODO 根据传入参数获取楼层
                                    .belongBuild(classroom.getBelongBuild()).floor(1).seats(classroom.getSeats())
                                    .classroomType(classroom.getClassroomType()).build());
                    if (oneClassroom != 1) {
                         throw new EasyEduException(DbError.Err_INSERT_DB_ERROR);
                    }
               }
          }
          return hasClassroom;
     }

     public void updateClassroom(ReqUpdateClassRoom reqUpdateClassRoom) {

          Classroom classroomById = classRoomDao.getClassroomById(reqUpdateClassRoom.getClassroomId());
          if (classroomById == null) {
               throw new EasyEduException(DeanServiceError.ERR_ClASS_ROOM_NOT_EXIST);
          }
          Classroom classroom = new Classroom();
          BeanUtils.copyProperties(reqUpdateClassRoom, classroom);
          Integer integer = classRoomDao.updateClassroom(classroom);
          if (integer != 1) {
               throw new EasyEduException(DbError.Err_UPDATE_DB_ERROR);
          }
     }

     public void deleteClassroom(Long classroomId) {

          Classroom classroomById = classRoomDao.getClassroomById(classroomId);
          if (classroomById == null) {
               throw new EasyEduException(DeanServiceError.ERR_ClASS_ROOM_NOT_EXIST);
          }
          Integer integer = classRoomDao.deleteClassroom(classroomId);
          if (integer != 1) {
               throw new EasyEduException(DbError.Err_DELETE_DB_ERROR);
          }
     }

     public List<BuildingAddress> getBuild() {

          return buildingAddressDao.getBuild();
     }
}
